package com.agritech.machine;

/**
 * 喷药机类
 * 实现了农业机械抽象类，提供喷药机特有的功能
 */
public class Sprayer extends AgriculturalMachine {
    private int pesticideCapacity; // 农药容量(升)
    private int currentPesticideAmount; // 当前农药量
    private double sprayRange; // 喷洒范围(米)
    private String pesticideType; // 农药类型

    public Sprayer(String id) {
        super(id, "智能喷药机");
        this.pesticideCapacity = 500;
        this.currentPesticideAmount = 500;
        this.sprayRange = 10.0; // 默认喷洒范围
        this.pesticideType = "通用型";
    }

    @Override
    public void start() {
        System.out.println(getType() + " [" + getId() + "] 启动中，检查系统...");
        if (currentPesticideAmount < 50) {
            System.out.println("警告: 农药余量不足，请及时补充！");
        }
        updateStatus("工作中");
        System.out.println(getType() + " [" + getId() + "] 已启动，准备喷洒作业");
    }

    @Override
    public void stop() {
        System.out.println(getType() + " [" + getId() + "] 正在停止作业...");
        updateStatus("空闲");
        System.out.println(getType() + " [" + getId() + "] 已停止，喷洒作业结束");
    }

    @Override
    public void performTask(String taskDetails) {
        System.out.println(getType() + " [" + getId() + "] 正在执行喷洒任务: " + taskDetails);
        System.out.println("当前使用农药类型: " + pesticideType + ", 喷洒范围: " + sprayRange + "米");
        consumePesticide(20); // 消耗农药
        consumeFuel(3.0); // 消耗燃油
    }

    /**
     * 调整喷洒范围
     * @param range 新的喷洒范围(米)
     */
    public void adjustSprayRange(double range) {
        System.out.println(getType() + " [" + getId() + "] 调整喷洒范围从 " + this.sprayRange + "米 到 " + range + "米");
        this.sprayRange = range;
    }

    /**
     * 更换农药类型
     * @param type 新的农药类型
     */
    public void changePesticideType(String type) {
        System.out.println(getType() + " [" + getId() + "] 更换农药类型从 '" + this.pesticideType + "' 到 '" + type + "'");
        this.pesticideType = type;
    }

    /**
     * 添加农药
     * @param amount 添加的农药量(升)
     */
    public void addPesticide(int amount) {
        this.currentPesticideAmount = Math.min(pesticideCapacity, currentPesticideAmount + amount);
        System.out.println(getType() + " [" + getId() + "] 已添加农药，当前农药量: " + currentPesticideAmount + "升");
    }

    /**
     * 消耗农药
     * @param amount 消耗的农药量(升)
     */
    private void consumePesticide(int amount) {
        this.currentPesticideAmount = Math.max(0, currentPesticideAmount - amount);
        System.out.println(getType() + " [" + getId() + "] 已消耗农药，剩余农药量: " + currentPesticideAmount + "升");
        if (currentPesticideAmount < 50) {
            System.out.println("警告: " + getType() + " [" + getId() + "] 农药余量不足，请及时补充！");
        }
    }

    /**
     * 获取当前农药量
     * @return 当前农药量(升)
     */
    public int getCurrentPesticideAmount() {
        return currentPesticideAmount;
    }

    /**
     * 获取喷洒范围
     * @return 喷洒范围(米)
     */
    public double getSprayRange() {
        return sprayRange;
    }

    /**
     * 获取农药类型
     * @return 农药类型
     */
    public String getPesticideType() {
        return pesticideType;
    }
}